from multiprocessing import Process,Queue
import os, time, random

#写数据进程执行代码
def write(q):
    print('Process to write %s' %os.getpid())
    for value in ['a', 'b', 'c']:
        print('Put %s to queue...' %value)
        q.put(value)
        time.sleep(random.random())

#读数据进程执行代码
def read(q):
    print('Process to read %s' % os.getpid())
    while True:
        value = q.get(True)
        print('Get %s from queue.' %value)

if __name__ == '__main__':
    # 父进程创建Queue,并传给各个子进程
    q = Queue()
    pw = Process(target = write, args = (q,))
    pr = Process(target = read, args = (q,))
    # 启动写数据子进程
    pw.start()
    # 启动读数据子进程
    pr.start()
    # 等待pw 结束
    pw.join()
    # pr是死循环，因此只能强制终止
    pr.terminate()